﻿using System.Threading.Tasks;
using System.Web.Http;
using Core.Models;
using Core.Models.Enums;
using Ninject;
using Service.App_Start;
using Service.Helpers;
using Service.Workflow.Game.Command;
using Service.Workflow.LearningWeight.Command;
using ServiceInterface.InputModels;
using ServiceInterface.ResponseModels;
using ShortBus;

namespace Service.Controllers
{
    public class AIController : ApiController
    {
        private readonly IMediator mediator;
 
        public AIController()
        {
           mediator = (IMediator)NinjectWebCommon.GetKernel().Get(typeof (IMediator));
        }

        [HttpPost]
        [Route("api/CreateGame")]
        public async Task<IHttpActionResult> CreateGame(NewGameInputModel inputModel)
        {
            var createGameCommand = new CreateGameCommand()
            {
                Player1Color = inputModel.Player1Color,
                Player2Color = inputModel.Player2Color,
                Player1Difficulty = inputModel.Player1Difficulty,
                Player2Difficulty = inputModel.Player2Difficulty,
                Player1Heuristics = inputModel.Player1Heuristics,
                Player2Heuristics = inputModel.Player2Heuristics
            };

            var response = await mediator.RequestAsync(createGameCommand);

            CurrentGameHelper.Instance.AddGame(response.Data.GameId, new GameState("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq"));
            
            var responseModel = new NewGameResponseModel
            {
                GameId = response.Data.GameId
            };

            return Ok(responseModel);
        }

        [HttpPost]
        [Route("api/FindMove")]
        public async Task<IHttpActionResult> FindMove(FindNextMoveInputModel inputInputModel)
        {
            //start the thread to stop the moveCommand with the remainingTime
            var moveCommand = new MoveGameCommand()
            {
                GameId = inputInputModel.GameId,
                MoveSource = inputInputModel.MoveSource,
                MoveTarget= inputInputModel.MoveTarget ,
                FullMove = inputInputModel.FullMove
            };

            var response = await mediator.RequestAsync(moveCommand);

            var responseModel = new NextMoveResponseModel()
            {
                MoveSource = response.Data.MoveSource ?? response.Data.FullMove.Substring(0,2),
                MoveTarget = response.Data.MoveTarget ?? response.Data.FullMove.Substring(2,2)
            };

            return Ok(responseModel);
        }

        [HttpPost]
        [Route("api/DebugFEN")]
        public async Task<IHttpActionResult> DebugFEN(string FEN)
        {
            int gameId = CurrentGameHelper.Instance.HighestId() + 1;
            CurrentGameHelper.Instance.AddGame(gameId, new GameState(FEN));

            //start the thread to stop the moveCommand with the remainingTime
            var moveCommand = new MoveGameCommand()
            {
                GameId = gameId,
                MoveSource = null,
                MoveTarget = null,
                FullMove = null
            };

            var response = await mediator.RequestAsync(moveCommand);

            var responseModel = new NextMoveResponseModel()
            {
                MoveSource = response.Data.MoveSource ?? response.Data.FullMove.Substring(0, 2),
                MoveTarget = response.Data.MoveTarget ?? response.Data.FullMove.Substring(2, 2)
            };

            return Ok(responseModel);
        }

        [HttpPost]
        [Route("api/LearnWeight")]
        public async Task<IHttpActionResult> LearnWeight(string pgn)
        { 
            var learnWeightCommand = new LearnWeightCommand()
            {
                pgn = pgn
            };

            var response = await mediator.RequestAsync(learnWeightCommand);

            return Ok(response);
        }
    }
}
